home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14438 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  63 lines

  1. Path: news1.sympatico.ca!news
  2. From: Gisele Swinson <gisele.swinson@sympatico.ca>
  3. Newsgroups: comp.lang.c
  4. Subject: Sorting a Binary File
  5. Date: 14 Apr 1996 21:15:59 GMT
  6. Organization: Sympatico
  7. Message-ID: <4krpuf$jfp@news1.sympatico.ca>
  8. NNTP-Posting-Host: ppp2022.on.sympatico.ca
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.22MII (Windows; I; 16bit)
  13.  
  14. I have declared a structure as follows:
  15.  
  16. #define max 5
  17.  
  18. struct customer
  19. {
  20.      int seatno;
  21.      char lastname[15];
  22. } Customer[max];
  23.  
  24. I have also initialized the structure as the seat numbers 1-5 and the last 
  25. name as blank
  26.  
  27. My program summarized is I enter a seat number then enter the name, when I 
  28. cam finished, save the data to a binary file. I want to sort the 
  29. structures in last name order.  I tried using bubble sort but I'm not 
  30. being very successful.
  31.  
  32. This is what I tried:
  33.  
  34. for(pass=1; pass<max; pass++)
  35.    for(i = 0; i<max-1; i++)
  36.  
  37.       if(Customer[i].lastname > Customer[i+1].lastname)
  38.       {
  39.           temp = Customer[i];
  40.           Customer[i] = Customer[i+1];
  41.           Customer[i+1] = temp;
  42.       }
  43.  
  44.       printf("\nSorted customer list");
  45.       printf("\nSeat number   Customer Name"):
  46.       
  47.       for(x=0; x<max; x++)
  48.            printf("\n%2d\t %s", Customer[x].seatno, Customer[x].lastname);
  49.  
  50.  
  51. I'm sorry this is quite detailed, but I am quit lost as to what I am doing 
  52. wrong.
  53.  
  54. If anyone can shed some light, I would truly appreciate it.
  55.  
  56. Thanks
  57.  
  58. Gisele
  59.  
  60.  
  61.  
  62.  
  63.